package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.dto.jz.ProductDetailVo;
import com.sxbbc.common.core.dto.jz.ProductVo;
import com.sxbbc.common.core.entity.jz.ProSkuDetail;
import com.sxbbc.common.core.entity.jz.ProcMain;
import com.sxbbc.common.core.mapper.jz.ActivitySeckillDetailMapper;
import com.sxbbc.common.core.mapper.jz.ProSkuDetailMapper;
import com.sxbbc.common.core.service.IProGroupConfigService;
import com.sxbbc.common.core.service.IProSkuDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.service.IProcMainService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 商品sku详情表 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-05-24
 */
@Service
public class ProSkuDetailServiceImpl extends ServiceImpl<ProSkuDetailMapper, ProSkuDetail> implements IProSkuDetailService {

    @Autowired
    private ProSkuDetailMapper proSkuDetailMapper;
    @Autowired
    private ActivitySeckillDetailMapper activitySeckillDetailMapper;
    @Autowired
    private IProcMainService procMainService;
    @Autowired
    private IProGroupConfigService proGroupConfigService;

    @Override
    public List<String> queryRepeatSkuCode(Integer bid, List<String> codeList,String proCode) {
        return proSkuDetailMapper.queryRepeatSkuCode(bid,codeList,proCode);
    }

    @Override
    public List<ProSkuDetail> querySkuListByProCode(String proCode) {
        QueryWrapper<ProSkuDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("pro_code",proCode).eq("status",StaticUtils.STATUS_YES);
        List<ProSkuDetail> skuList = super.list(wrapper);
        return skuList;
    }

    @Override
    public ProductDetailVo selectPriceAndStock(ProductVo productVo) {
        QueryWrapper<ProSkuDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("pro_code",productVo.getProCode()).eq("spec_name",productVo.getSku()).eq("status", StaticUtils.STATUS_YES);
        ProSkuDetail proSkuDetail = super.getOne(wrapper);
        ProcMain procMain = procMainService.selectOneByProCode(productVo.getProCode());
        if (procMain == null) {
            throw new ThrowJsonException("商品信息不存在");
        }
        ProductDetailVo detailVo = new ProductDetailVo();
        if (proSkuDetail != null) {
            detailVo.setPrice(proSkuDetail.getPrice());
            detailVo.setStock(proSkuDetail.getQty());
            detailVo.setSkuId(proSkuDetail.getId());
            detailVo.setPoint(proSkuDetail.getPoint());
            //图片处理
            if(StringUtils.isNotBlank(proSkuDetail.getImgUrl())) {
            	detailVo.setProUrl(proSkuDetail.getImgUrl());
            }else {
            	detailVo.setProUrl(procMain.getProUrl());
            }
        }
        return detailVo;
    }

    @Override
    public List<ProSkuDetail> queryProductDetailList(String proCode,Integer bid) {
        return baseMapper.queryProductDetailList(proCode,bid);
    }

    @Override
    public RestResponse handleDeleteSku(String skuVal, String proCode) {
        ProcMain procMain = procMainService.selectOneByProCode(proCode);
        if (procMain == null) {
            return GetRest.getFail("该商品不存在");
        }
        QueryWrapper<ProSkuDetail> wrapper = new QueryWrapper<>();
        wrapper.like("spec_name",skuVal).eq("status",StaticUtils.STATUS_YES);
        List<ProSkuDetail> list = super.list(wrapper);
        Integer stock = 0;
        boolean handle = false;
        if (!CollectionUtils.isEmpty(list) && list.size() > 0) {
            for (ProSkuDetail proSkuDetail : list) {
                proSkuDetail.setStatus(StaticUtils.STATUS_NO);
                stock += proSkuDetail.getQty();
                proSkuDetail.setQty(0);
                handle = super.updateById(proSkuDetail);
            }
            if (!handle) {
                throw new ThrowJsonException("处理删除规格值失败");
            }
        }
        procMain.setQty(procMain.getQty() - stock);
        handle = procMainService.updateById(procMain);
        if (!handle) {
            throw new ThrowJsonException("处理商品库存失败");
        }
        return GetRest.getSuccess("操作成功");
    }

	@Override
	public int updateQtyVersion(ProSkuDetail sku) {
		return baseMapper.updateQtyVersion(sku);
	}

	@Override
	public void updateSaleqty(ProSkuDetail detail) {
		baseMapper.updateSaleqty(detail);
		
	}
}
